package com.kye.express.business.customer_code.bind_customer_code;

import com.kye.express.contract.base.BasePresenter;
import com.kye.express.contract.base.view.ErrorView;
import com.kye.express.contract.base.view.IView;
import com.kye.express.contract.base.view.LoadingView;
import com.kye.model.OpenApiResultObserver;
import com.kye.model.UserSession;
import com.kye.model.base.RetrofitManager;
import com.kye.model.bean.OpenApiResult;
import com.kye.model.bean.user.User;
import com.kye.model.bean.user.request.RequestUserBindCustomerCode;
import com.kye.model.service.UserService;

import io.reactivex.SingleObserver;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;

public interface BindCustomerCodeContract {

    interface BindCustomerCodeView extends IView, LoadingView, ErrorView {

        void onUser(User user);

        void onSuccess();

        void onCompanyName(String s);
    }

    class BindCustomerCodePresenter extends BasePresenter<BindCustomerCodeView> {
        @Override
        public void bind(BindCustomerCodeView view) {
            super.bind(view);

            UserSession.getInstance()
                    .getUserForRx()
                    .compose(getView().<User>bindToLifecycle())
                    .subscribe(new Consumer<User>() {
                        @Override
                        public void accept(User user) throws Exception {
                            getView().onUser(user);
                        }
                    });
        }

        public void getCompanyName() {
            UserSession.getInstance()
                    .getUserForRx()
                    .firstOrError()
                    .map(new Function<User, String>() {
                        @Override
                        public String apply(User user) throws Exception {
                            String customCode = user.getCustomCode();
                            for (User.Address address : user.getLastAddress()) {
                                if (customCode.equals(address.getCompanyCode())) {
                                    return address.getCompanyName();
                                }
                            }
                            return null;
                        }
                    })
                    .compose(getView().<String>bindToLifecycleDestroy())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new SingleObserver<String>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(String s) {
                            getView().onLoaded();

                            getView().onCompanyName(s);
                        }

                        @Override
                        public void onError(Throwable e) {
                            getView().onLoaded();
                        }
                    });
        }

        public void bindCustomerCode(String phone, String code, String password) {
            RetrofitManager.getInstance()
                    .createOpenApiService(UserService.class)
                    .bindCustomerCode(new RequestUserBindCustomerCode(phone, code, password))
                    .compose(getView().<OpenApiResult<User>>bindToLifecycleDestroy())
                    .doOnSuccess(new Consumer<OpenApiResult<User>>() {
                        @Override
                        public void accept(OpenApiResult<User> userOpenApiResult) throws Exception {
                            User user = userOpenApiResult.getData();
                            if (user == null) return;

                            UserSession.getInstance().setUser(user);
                        }
                    })
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new OpenApiResultObserver<User>() {
                        @Override
                        public void onOtherError(Throwable e) {
                            getView().onError();
                        }

                        @Override
                        public void onLoading(Disposable d) {
                            getView().onLoading();
                        }

                        @Override
                        public void onSuccess(OpenApiResult<User> result, User user) {
                            getView().onSuccess();
                        }

                        @Override
                        public void onApiError(OpenApiResult<User> result, int code, String msg) {
                            getView().onError(msg);
                        }

                        @Override
                        public void onLoaded() {
                            getView().onLoaded();
                        }
                    });
        }
    }
}
